//Flag between pool and question
var flag = true;


/**
 * Question
 * 
 * @param i
 */

//True/False Template
var truefalse = "<div class=\"question\" id=\"edit\">"
		+ "<div class=\"question_type\">"
		+ "<select id=\"qType\" class=\"form-control\" style=\"width: 150px;\""
		+ "onchange=\"changeType()\">"
		+ "<option value=\"1\">True/False</option>"
		+ "<option value=\"2\">Single Choice</option>"
		+ "<option value=\"3\">Multiple Choice</option>"
		+ "</select>"
		+ "</div>"
		+ "<div class=\"question_number\">"
		+ "<ul>"
		+ "<li><h6 class=\"questionId hidden\">JAVA_OOP_002</h6>"
		+ "<h5 class=\"questionNum\">Question 2:</h5></li>"
		
		//+ "<li><b><i><input id=\"txtPoint\" type=\"text\" class=\"form-control point\" style=\"width: 60px; display: inline; margin-left: 10px; margin-bottom: 5px;\"> Point</b></i></li><li class=\"error\" id=\"pointErr\" style=\"margin-left: 10px;\"></li>"
		
		+ "</ul>"
		+ "</div>"
		+ "<div class=\"question_name\">"
		+ "<textarea id=\"q_name\" rows=\"5\" cols=\"75\"></textarea><h5 id=\"questionNameError\" class=\"error\"></h5>"
		+ "</div>"
		+ "<table class=\"question_answer\">"
		+ "<tr class=\"answer\">"
		+ "<td><label><input type=\"radio\" name=\"q\" value=\"A\" />A. True</label></td>"
		+ "<td></td>"
		+ "</tr>"
		+ "<tr class=\"answer\">"
		+ "<td><label><input type=\"radio\" name=\"q\" value=\"B\" />B. False</label></td>"
		+ "<td></td>"
		+ "				</tr>"
		+ "			</table>"
		+ "<table class=\"question_answer\">"
		+ "				<tr>"
		+ "<td></td>"
		+ "<td><a href=\"\" id=\"updateQuestion\" class=\"btn btn-primary btn-sm\""
		+ ">Update</a><a href=\"\" class=\"btn btn-primary btn-sm\""
		+ " id=\"cancelQuestion\">Cancel</a></td>" + "</tr>" + "			</table>"
		+ "		</div>"

// Multiple choice Templete
var multiChoice = "<div class=\"question\" id=\"edit\">"
		+ "<div class=\"question_type\">"
		+ "<select id=\"qType\" class=\"form-control\" style=\"width: 150px;\""
		+ "onchange=\"changeType()\">"
		+ "<option value=\"1\">True/False</option>"
		+ "<option value=\"2\">Single Choice</option>"
		+ "<option value=\"3\">Multiple Choice</option>"
		+ "</select>"
		+ "</div>"
		+ "<div class=\"question_number\">"
		+ "<ul>"
		+ "<li><h6 class=\"questionId hidden\">JAVA_OOP_002</h6>"
		+ "<h5 class=\"questionNum\">Question 2:</h5></li>"
		
		//+ "<li><b><i><input id=\"txtPoint\" type=\"text\" class=\"form-control point\" style=\"width: 60px; display: inline; margin-left: 10px; margin-bottom: 5px;\"> Point</b></i></li><li class=\"error\" id=\"pointErr\" style=\"margin-left: 10px;\"></li>"
		
		+ "</ul>"
		+ "</div>"
		+ "<div class=\"question_name\">"
		+ "<textarea id=\"q_name\" rows=\"5\" cols=\"75\"></textarea><h5 id=\"questionNameError\" class=\"error\"></h5>"
		+ "</div>"
		+ "<table class=\"question_answer\">"
		+ "<tr class=\"answer\">"
		+ "<td><label><input type=\"checkbox\" name=\"q\" value=\"A\" />A.</label></td>"
		+ "<td><textarea id=\"A\" rows=\"3\" cols=\"75\"></textarea><h5 id=\"answerA\" class=\"error\"></h5></td>"
		+ "</tr>"
		+ "<tr class=\"answer\">"
		+ "<td><label><input type=\"checkbox\" name=\"q\" value=\"B\" />B.</label></td>"
		+ "<td><textarea id=\"B\" rows=\"3\" cols=\"75\"></textarea><h5 id=\"answerB\" class=\"error\"></h5></td>"
		+ "</tr>"
		+ "<tr class=\"answer\">"
		+ "<td><label><input type=\"checkbox\" name=\"q\" value=\"C\" />C.</label></td>"
		+ "<td><textarea id=\"C\" rows=\"3\" cols=\"75\"></textarea><h5 id=\"answerC\" class=\"error\"></h5></td>"
		+ "</tr>"
		+ "<tr class=\"answer\">"
		+ "<td><label><input type=\"checkbox\" name=\"q\" value=\"D\" />D.</label></td>"
		+ "<td><textarea id=\"D\" rows=\"3\" cols=\"75\"></textarea><h5 id=\"answerD\" class=\"error\"></h5></td>"
		+ "</tr>"
		+ "</table>"
		+ "<table class=\"question_answer\">"
		+ "<tr>"
		+ "<td></td>"
		+ "<td><a href=\"#\""
		+ "onclick=\"javascript:addAnswer(0); return false;\">Add Answer</a></td>"
		+ "</tr>"
		+ "<tr>"
		+ "<td></td>"
		+ "<td><a href=\"\" id=\"updateQuestion\" class=\"btn btn-primary btn-sm\""
		+ ">Update</a><a href=\"\" class=\"btn btn-primary btn-sm\""
		+ " id=\"cancelQuestion\">Cancel</a></td>" + "</tr>" + "</table>"
		+ "</div>";

// Single choice Template
var singleChoice = "<div class=\"question\" id=\"edit\">"
		+ "<div class=\"question_type\">"
		+ "<select id=\"qType\" class=\"form-control\" style=\"width: 150px;\""
		+ "onchange=\"changeType()\">"
		+ "<option value=\"1\">True/False</option>"
		+ "<option value=\"2\">Single Choice</option>"
		+ "<option value=\"3\">Multiple Choice</option>"
		+ "</select>"
		+ "</div>"
		+ "<div class=\"question_number\">"
		+ "<ul>"
		+ "<li><h6 class=\"questionId hidden\">JAVA_OOP_002</h6>"
		+ "<h5 class=\"questionNum\">Question 2:</h5></li>"
		
		//+ "<li><b><i><input id=\"txtPoint\" type=\"text\" class=\"form-control point\" style=\"width: 60px; display: inline; margin-left: 10px; margin-bottom: 5px;\"> Point</b></i></li><li class=\"error\" id=\"pointErr\" style=\"margin-left: 10px;\"></li>"
		
		+ "</ul>"
		+ "</div>"
		+ "<div class=\"question_name\">"
		+ "<textarea id=\"q_name\" rows=\"5\" cols=\"75\"></textarea><h5 id=\"questionNameError\" class=\"error\"></h5>"
		+ "</div>"
		+ "<table class=\"question_answer\">"
		+ "<tr class=\"answer\">"
		+ "<td><label><input type=\"radio\" name=\"q\" value=\"A\" />A.</label></td>"
		+ "<td><textarea id=\"A\" rows=\"3\" cols=\"75\"></textarea><h5 id=\"answerA\" class=\"error\"></h5></td>"
		+ "</tr>"
		+ "<tr class=\"answer\">"
		+ "<td><label><input type=\"radio\" name=\"q\" value=\"B\" />B.</label></td>"
		+ "<td><textarea id=\"B\" rows=\"3\" cols=\"75\"></textarea><h5 id=\"answerB\" class=\"error\"></h5></td>"
		+ "</tr>"
		+ "<tr class=\"answer\">"
		+ "<td><label><input type=\"radio\" name=\"q\" value=\"C\" />C.</label></td>"
		+ "<td><textarea id=\"C\" rows=\"3\" cols=\"75\"></textarea><h5 id=\"answerC\" class=\"error\"></h5></td>"
		+ "</tr>"
		+ "<tr class=\"answer\">"
		+ "<td><label><input type=\"radio\" name=\"q\" value=\"D\" />D.</label></td>"
		+ "<td><textarea id=\"D\" rows=\"3\" cols=\"75\"></textarea><h5 id=\"answerD\" class=\"error\"></h5></td>"
		+ "</tr>"
		+ "</table>"
		+ "<table class=\"question_answer\">"
		+ "<tr>"
		+ "<td></td>"
		+ "<td><a href=\"#\""
		+ "onclick=\"javascript:addAnswer(0); return false;\">Add Answer</a></td>"
		+ "</tr>"
		+ "<tr>"
		+ "<td></td>"
		+ "<td><a href=\"\" id=\"updateQuestion\" class=\"btn btn-primary btn-sm\""
		+ ">Update</a><a href=\"\" class=\"btn btn-primary btn-sm\""
		+ " id=\"cancelQuestion\">Cancel</a></td>" + "</tr>" + "</table>"
		+ "</div>";

var btnAddQuestion = "<a href=\"\" id=\"addQuestion\" class=\"btn btn-primary btn-sm\">Add</a>";
var lineAddQuestion = "<div class=\"add_question\"><a class=\"addQuestion\" href=\"\">Add Question</a></div>";

// Backup
var backupEditQuestion;

// Flag add question
var flagEditQuestion = true;
var flagAddQuestion = true;
// Show template for add question when click add question line
$(document).on("click", ".addQuestion", function() {
	if (flagAddQuestion == false || flagEditQuestion == false) {
		$("#dialog-not-allow").dialog("open");
		return false;
	}
	flagAddQuestion = false;
	var question = $(this).parent();
	$(truefalse + lineAddQuestion).insertAfter(question);
	$("#updateQuestion").replaceWith(btnAddQuestion);
	autoArrange();
	return false;
});

//True false question
var truefalseQuestion = "<div class=\"question\"><div class=\"question_number\"><ul><li><h6 class=\"questionId hidden\">"
		+ "</h6><h5 class=\"questionNum\">Question 2:</h5></li>"
		//+ "<li><b style=\"margin-left: 10px;\"><i class=\"point\"> Point</i></b></li>"
		+ "<li class=\"pull-right\"><a class=\"editQuestion\" href=\"\">Edit</a>"
		+ "<a class=\"removeQuestion\" href=\"\">Remove</a></li></ul></div><div class=\"question_name\"></div><div class=\"question_answer\">"
		+ "<div><input type=\"radio\" value=\"A\"disabled>A. True</div><div><input type=\"radio\" value=\"B\" disabled>B. False</div></div></div>";
// Mutil Choice
var multiChoiceQuestion = "<div class=\"question\"><div class=\"question_number\"><ul><li><h6 class=\"questionId hidden\"></h6>"
		+ "<h5 class=\"questionNum\">Question 1:</h5>"
		//+ "<li><b style=\"margin-left: 10px;\"><i class=\"point\"> Point</i></b></li>"
		+ "</li><li class=\"pull-right\"><a class=\"editQuestion\" href=\"\">Edit</a>"
		+ "<a class=\"removeQuestion\" href=\"\">Remove</a></li></ul></div><div class=\"question_name\"></div><div class=\"question_answer\">"
		+ "<div><input type=\"checkbox\" value=\"A\" disabled></div><div><input type=\"checkbox\" value=\"B\" disabled></div><div>"
		+ "<input type=\"checkbox\" value=\"C\" disabled></div><div><input type=\"checkbox\" value=\"D\" disabled></div></div></div>";

var singleChoiceQuestion = "<div class=\"question\"><div class=\"question_number\"><ul><li><h6 class=\"questionId hidden\"></h6>"
		+ "<h5 class=\"questionNum\">Question 1:</h5></li>"
		//+ "<li><b style=\"margin-left: 10px;\"><i class=\"point\"> Point</i></b></li>"
		+ "<li class=\"pull-right\"><a class=\"editQuestion\" href=\"\">Edit</a>"
		+ "<a class=\"removeQuestion\" href=\"\">Remove</a></li></ul></div><div class=\"question_name\"></div><div class=\"question_answer\">"
		+ "<div><input type=\"radio\" value=\"A\" disabled></div><div><input type=\"radio\" value=\"B\" disabled></div><div>"
		+ "<input type=\"radio\" value=\"C\" disabled></div><div><input type=\"radio\" value=\"D\" disabled></div></div></div>";

// Save when click add new question button
$(document).on("click", "#addQuestion", function() {
	//Validate question
	var error = false;
	
	var question = "";
	var answer = "";
	var rightAnswer = "";
	var point = 1;

	//point = $("#txtPoint").val();
	question = $("#q_name").val()
	var type = $("#qType").val();
	// Right Answer
	if (type == 3) {// Checkbox
		var rightAnswers = $("#edit .question_answer input[name=q]:checked");
		rightAnswers.each(function() {
			rightAnswer += $(this).val();
		});
	} else {// Radio
		rightAnswer = $("#edit .question_answer input[name=q]:checked").val();
		if(rightAnswer == undefined){
			rightAnswer = "";
		}
	}

	// Answer
	if (type != 1) {
		answer += ("A_" + $("#A").val());
		answer += (";B_" + $("#B").val());
		answer += (";C_" + $("#C").val());
		answer += (";D_" + $("#D").val());
		
		if($("#A").val() == ""){
			$("#answerA").text("Answer can not be blank!");
			error = true;
		}
		else{
			$("#answerA").text("");
		}
		
		if($("#B").val() == ""){
			$("#answerB").text("Answer can not be blank!");
			error = true;
		}
		else{
			$("#answerB").text("");
		}
		
		if($("#C").val() == ""){
			$("#answerC").text("Answer can not be blank!");
			error = true;
		}
		else{
			$("#answerC").text("");
		}
		
		if($("#D").val() == ""){
			$("#answerD").text("Answer can not be blank!");
			error = true;
		}
		else{
			$("#answerD").text("");
		}
	}
	
//Validate point
	
	if(isNaN(point) || point <= 0){
		$("#pointErr").text("Please, enter a number greater than 0!");
		error = true;
	}else{
		$("#pointErr").text("");
	}
	
	//Validate question
	if(question == ""){
		$("#questionNameError").text("Question can not be blank!");
		error = true;
	}else{
		$("#questionNameError").text("");
	}
	
	if(error == true)
		return false;
	else{
		error = false;
	}
	//Right answer Validate
	if(rightAnswer == ""){
		$("#questionNameError").text("Please, choose right answer!");
		return false;
	}else{
		$("#questionNameError").text("");
	}
	// Get point??
	var requestData = {
		questionId : null,
		question : question,
		answer : answer,
		point : point,
		rightAnswer : rightAnswer,
		type : type
	};
	// Get pool of this question
	var questionPool;
	questionPool = $(this).parents(".question_pool");
	// Get poolId of this question
	var poolId;
	poolId = questionPool.find(".poolId").text();

	$.ajax({
		url : "../"+poolId+"/question/addToPool",
		data : JSON.stringify(requestData),
		dataType : 'json',
		contentType : 'application/json; charset=utf-8',
		type : "POST",
		error : function(error) {
			alert(error);
		},
		success : function(data) {
			// Process after add?
			showQuestion(type, data);
			autoArrange();
		},
		async : true,
		processData : false
	});
	flagAddQuestion = true;
	flagEditQuestion = true;
	autoArrange();
	return false;
});

// Show data after add
function showQuestion(type, data) {
	var question = $("#edit");
	var answer = data.answer.split(";");
	var rightAnswer = data.rightAnswer;

	if (type == 1) {
		// True/False
		question = $(truefalseQuestion).replaceAll("#edit");
		//question.find(".point").text(data.point + " Point");
		question.find(".questionId").text(data.questionId);
		question.find(".question_name").text(data.question);
		// Show check
		question.find("input[value=" + rightAnswer + "]").prop("checked", true);
		return false;
	} else if (type == 2) {
		// Single Choice
		question = $(singleChoiceQuestion).replaceAll("#edit");
		question.find("input[value=A]").parent().html(
				"<input type=\"radio\" value=\"A\" disabled>"
						+ answer[0].substring(2, answer[0].length));
		question.find("input[value=B]").parent().html(
				"<input type=\"radio\" value=\"B\" disabled>"
						+ answer[1].substring(2, answer[1].length));
		question.find("input[value=C]").parent().html(
				"<input type=\"radio\" value=\"C\" disabled>"
						+ answer[2].substring(2, answer[2].length));
		question.find("input[value=D]").parent().html(
				"<input type=\"radio\" value=\"D\" disabled>"
						+ answer[3].substring(2, answer[3].length));
	} else {
		// Multi Choice
		question = $(multiChoiceQuestion).replaceAll("#edit");
		question.find("input[value=A]").parent().html(
				"<input type=\"checkbox\" value=\"A\" disabled>"
						+ answer[0].substring(2, answer[0].length));
		question.find("input[value=B]").parent().html(
				"<input type=\"checkbox\" value=\"B\" disabled>"
						+ answer[1].substring(2, answer[1].length));
		question.find("input[value=C]").parent().html(
				"<input type=\"checkbox\" value=\"C\" disabled>"
						+ answer[2].substring(2, answer[2].length));
		question.find("input[value=D]").parent().html(
				"<input type=\"checkbox\" value=\"D\" disabled>"
						+ answer[3].substring(2, answer[3].length));
	}
	//question.find(".point").text(data.point + " Point");
	question.find(".questionId").text(data.questionId);
	question.find(".question_name").text(data.question);
	// Selected
	for (var i = 0; i < rightAnswer.length; i++) {
		question.find("input[value=" + rightAnswer[i] + "]").prop("checked",
				true);
	}
	return false;
}

// Edit when click edit question
$(document).on("click", ".editQuestion", function() {
	// Get question id;
	if (flagAddQuestion == false || flagEditQuestion == false) {
		$("#dialog-not-allow").dialog("open");
		return false;
	}
	flagEditQuestion = false;
	var question = $(this).parents(".question");
	var questionId = question.find(".questionId").text();
	// Using ajax get Server question
	$.ajax({
		url : "../noPoolId/question/" + questionId,
		type : "GET",
		dataType : "json",
		contentType : "application/json; charset=utf-8",
		error : function(error) {
			alert(error + "Error No Processing");
		},
		success : function(data) {
			backupEditQuestion = question.clone();
			var answer = data.answer.split(";");
			if (data.type == 1) {
				question.replaceWith(truefalse);
			} else if (data.type == 2) {
				question.replaceWith(singleChoice);
			} else {
				question.replaceWith(multiChoice);
			}

			$("#qType").val(data.type);
			//$("#txtPoint").val(data.point);
			$("#qType").prop("disabled", true);
			$("#q_name").val(data.question);
			$("#edit").find(".questionId").text(questionId);

			// Show database
			if (data.type == 1) {
				// set true or false
			} else {
				$("#A").val(answer[0].substring(2, answer[0].length));
				$("#B").val(answer[1].substring(2, answer[1].length));
				$("#C").val(answer[2].substring(2, answer[2].length));
				$("#D").val(answer[3].substring(2, answer[3].length));
			}
			// Set old right Answer
			if (data.type != 1) {
				for(var i = 0; i < data.rightAnswer.length; i++){
					$("#"+data.rightAnswer[i]).parent().siblings().find("input").prop("checked", true);
				}
			} else {// True false
				$("#edit").find("input[value="+data.rightAnswer+"]").prop("checked", true);
			}
			autoArrange();
		}
	});
	return false;
});

// Update when click update in edit action
$(document).on("click", "#updateQuestion", function() {
	//Validate
	var error = false;
	
	var question = "";
	var answer = "";
	var rightAnswer = "";
	var point = 1;
	//point = $("#txtPoint").val();
	question = $("#q_name").val()
	var type = $("#qType").val();
	// Right Answer
	if (type == 3) {// Checkbox
		var rightAnswers = $("#edit .question_answer input[name=q]:checked");
		rightAnswers.each(function() {
			rightAnswer += $(this).val();
		});
	} else {// Radio
		rightAnswer = $("#edit .question_answer input[name=q]:checked").val();
		if(rightAnswer == undefined){
			rightAnser = "";
		}
	}
	
	// Answer
	if (type != 1) {
		answer += ("A_" + $("#A").val());
		answer += (";B_" + $("#B").val());
		answer += (";C_" + $("#C").val());
		answer += (";D_" + $("#D").val());
		
		if($("#A").val() == ""){
			$("#answerA").text("Answer can not be blank!");
			error = true;
		}
		else{
			$("#answerA").text("");
		}
		
		if($("#B").val() == ""){
			$("#answerB").text("Answer can not be blank!");
			error = true;
		}
		else{
			$("#answerB").text("");
		}
		
		if($("#C").val() == ""){
			$("#answerC").text("Answer can not be blank!");
			error = true;
		}
		else{
			$("#answerC").text("");
		}
		
		if($("#D").val() == ""){
			$("#answerD").text("Answer can not be blank!");
			error = true;
		}
		else{
			$("#answerD").text("");
		}
	}
	
//Validate point
	
	if(isNaN(point) || point <= 0){
		$("#pointErr").text("Please, enter a number greater than 0!");
		error = true;
	}else{
		$("#pointErr").text("");
	}
	
	//Validate question
	if(question == ""){
		$("#questionNameError").text("Question can not be blank!");
		error = true;
	}else{
		$("#questionNameError").text("");
	}
	
	if(error == true)
		return false;
	else{
		error = false;
	}
	
	//Right answer Validate
	if(rightAnswer == ""){
		$("#questionNameError").text("Please, choose right answer!");
		return false;
	}else{
		$("#questionNameError").text("");
	}
	
	// Get question id
	var questionId = $("#edit").find(".questionId").text();
	// Get point??
	var requestData = {
		questionId : questionId,
		question : question,
		answer : answer,
		point : point,
		rightAnswer : rightAnswer,
		type : type
	};

	$.ajax({
		url : "../noPoolId/question/edit/" + questionId,
		data : JSON.stringify(requestData),
		dataType : 'json',
		contentType : 'application/json; charset=utf-8',
		type : "POST",
		error : function(error) {
			alert(error);
		},
		success : function(data) {
			showQuestion(type, data);
			autoArrange();
		},
		async : true,
		processData : false
	});
	// Process after update?
	flagAddQuestion = true;
	flagEditQuestion = true;
	autoArrange();
	return false;
});

// Remove question
var deleteQuestion;

$(document).on(
		"click",
		".removeQuestion",
		function() {
			if (flagAddQuestion == false || flagEditQuestion == false) {
				$("#dialog-not-allow").dialog("open");
				return false;
			}
			deleteQuestion = $(this).parents(".question");
			$("#dialog-confirm").dialog("open");
			return false;
		});

// Cancel
$(document).on("click", "#cancelQuestion", function() {
	var question = $("#edit");
	var lineAddQuestion = question.next();
	if (flagAddQuestion == false) {
		// just remove new question
		question.remove();
		lineAddQuestion.remove();
	} else {
		// backup to previous state
		question.replaceWith(backupEditQuestion);
		// lineAddQuestion.remove();
	}
	flagAddQuestion = true;
	flagEditQuestion = true;
	autoArrange();
	return false;
});

$(document).on("change", "#qType", function() {
	var question = $("#edit");
	var type = $("#qType").val();
	if (type == 1) {
		question.replaceWith(truefalse);
	} else if (type == 2) {
		question.replaceWith(singleChoice);
	} else {
		question.replaceWith(multiChoice);
	}
	try {
		$("#updateQuestion").replaceWith(btnAddQuestion);
	} catch (e) {

	}
	$("#qType").val(type);
	autoArrange();
	return false;
});

function autoArrange(){
	var questions = $("#main_content").find(".question");
	for(var i = 0; i < questions.length; i++){
		var question = $(".question:eq("+i+")");
		var questionNum = question.find(".questionNum");
		questionNum.text("Question " + (i+1) + ":");
	}
};

//Delete Question
$(document).ready(function() {
	createDialogNotAllow();
	createDialogConfirm();
	createDialogAlert();
	createDialogOk();
	autoArrange();
});

//Delete Confirm

function createDialogConfirm() {
	var testId = $("#testId").text();
	var testName = $("#testName").text();
	$("#dialog-confirm").dialog({
		autoOpen : false,
		resizable : false,
		modal : true,
		buttons : {
			"Delete" : function() {
				var question = deleteQuestion;
				var questionId = question.find(".questionId").text();
				var lineAddQuestion;
				// just remove html when add new question
				if (flagAddQuestion == false) {
					// Remove html but not call controller
					lineAddQuestion = question.next();
					question.remove();
					lineAddQuestion.remove();
					flagAddQuestion = true;
					flagEditQuestion = true;
					flag = true;
					return false;
				} else {
					$.ajax({
						url : '../noPoolId/question/delete/' + questionId,
						type : "GET",
						error : function(jqXHR, textStatus, errorThrown) {
							$("#dialog-alert").dialog("open");
							return false;
						},
						success : function(data) {
							if (data == "OK") {
								// Remove when #edit is avaiable
								if (flagEditQuestion == false) {
									lineAddQuestion = question.next();
									lineAddQuestion.remove();
									question.remove();
								} else {
									question.next().remove(); // Remove line add
									// question after
									// question
									question.remove();// remove question
								}
								flagAddQuestion = true;
								flagEditQuestion = true;
								flag = true;
								$("#dialog-ok").dialog("open");
								autoArrange();
							} else
								alert("Error");
							return false;
						},
					});
				}
				$(this).dialog("close");
			},
			Cancel : function() {
				$(this).dialog("close");
			}
		}
	});
};
function createDialogAlert() {
	$("#dialog-alert").dialog({
		autoOpen : false,
		resizable : false,
		modal : true,
		buttons : {
			OK : function() {
				$(this).dialog("close");
			}
		}
	});
};

function createDialogOk() {
	$("#dialog-ok").dialog({
		autoOpen : false,
		resizable : false,
		modal : true,
		buttons : {
			OK : function() {
				$(this).dialog("close");
			}
		}
	});
};

function createDialogNotAllow() {
	$("#dialog-not-allow").dialog({
		autoOpen : false,
		resizable : false,
		modal : true,
		buttons : {
			OK : function() {
				$(this).dialog("close");
			}
		}
	});
};
